/**
 * Copyright (c) 2010 by Kevin Bierhoff.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *    1. Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *    2. Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *    3. Neither the names of the authors nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package com.googlecode.syper.javasyp.formulae;

/**
 * A heap access in the pre-image of the heap, meaning an access into the heap
 * as it was at the beginning of the executing method.
 * @author kevin
 * @since Feb 6, 2010
 */
public final class Pre implements Ref {

	private final Ref aged;
	private final int arrayDim;

	public Pre(Ref aged) {
		this(aged, 0);
	}

	public Pre(Ref aged, int arrayDim) {
		super();
		if (arrayDim < 0)
			throw new IllegalArgumentException("Cannot be negative: " + arrayDim);
		this.aged = aged;
		this.arrayDim = arrayDim;
	}

	public Ref getAged() { return aged; }
	public int getArrayDim() { return arrayDim; }

	@Override
	public <T> T dispatch(FormulaVisitor<T> visitor) {
		return visitor.visit(this);
	}

	@Override
	public boolean isBoolean() {
		if (arrayDim != 0)
			throw new IllegalStateException("This is an intermediary object: " + this);
		return aged.isBoolean();
	}

	@Override
	public String toString() {
		if (arrayDim == 0)
			return "old(" + aged + ")";
		StringBuilder result = new StringBuilder("old(");
		result.append(aged);
		for (int i = 0; i < arrayDim; ++i)
			result.append("[]");
		result.append(')');
		return result.toString();
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((aged == null) ? 0 : aged.hashCode());
		result = prime * result + arrayDim;
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Pre other = (Pre) obj;
		if (aged == null) {
			if (other.aged != null)
				return false;
		} else if (!aged.equals(other.aged))
			return false;
		if (arrayDim != other.arrayDim)
			return false;
		return true;
	}

}
